package com.yushutao.menu;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yushutao.menu.beans.OrderBean;
import com.yushutao.menu.beans.UserBean;
import com.yushutao.menu.dao.IOrder;
import com.yushutao.menu.dao.IUser;
import com.yushutao.menu.dao.impl.OrderDAO;
import com.yushutao.menu.dao.impl.UserDAO;

@Controller
@RequestMapping(value = "servant")
public class ServantController {
	
	private static final Logger logger = LoggerFactory.getLogger(ServantController.class);
	//private static final IDish dishDAO = new DishDAO();
	private static final IUser userDAO = new UserDAO();
	private static final IOrder orderDAO = new OrderDAO();
	
	/**
	 * 显示需要结账的顾客
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String showIndex(Model model) {
		logger.debug("req showIndex");
		List<UserBean> all = userDAO.getUserReadyToPay();
		for(UserBean ub:all){
			double allCount = 0;
			List<OrderBean> allO = orderDAO.getOrderByUserId(ub.getId());
			for(OrderBean b:allO){
				allCount+=b.getPrize();
			}
			ub.setAllPay(allCount);
		}
		model.addAttribute("users", all);
		return "servant/index";
	}
	/**
	 * 处理结账
	 */
	@RequestMapping(value = "/doFinishUser/{uCode}", method = RequestMethod.GET)
	public String doFinishUser(@PathVariable("uCode")int uCode,Model model) {
		logger.debug("req doFinishUser");
		userDAO.removeUserById(uCode);
		return "redirect:/servant/";
	}
}
